
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/


#pragma once

#include "RTRay.h"
#include <vector>
#include <map>
#include <string>
using namespace std;

namespace RT
{
	//forward declarations
	class RTIPrimitive;
	class RTILight;
	class ITexture;
	class RTMaterial;
	class RTIProcTexture;
	class RTTexture;

	/*Scene that the raytracer rendered*/
	class RTScene
	{
	private:
		//singleton variables
		static RTScene* m_instance;

		//storage for RTPrimitives
		vector<RTIPrimitive*> m_primitives;

		//storage for lights
		vector<RTILight*> m_lights;

		//storage for materials
		map<string, RTMaterial*> m_materials;

		//storage for texture
		vector<RTIProcTexture*> m_textures;

		//storage for file texture
		vector<RTTexture*> m_fileTexture;

		//constructor
		RTScene();

		//destructor
		~RTScene();

		//scene global ambient light
		RTColor m_globalAmbientLight;

	public:
		/*Return the singleton*/
		static RTScene& getInstance();

		/*Close the singleton*/
		void close();

		/*Return a primitive*/
		RTIPrimitive* getPrimitive(I32 index)const;

		/*Return a material*/
		RTMaterial* getMaterial(const string& materialName)const;

		//Return the light with the index id. Return 0 if the index is out of range.
		RTILight* getLight(U32 id)const;

		/*Delete a primitive*/
		RTIPrimitive* removePrimitive(I32 index);

		//Test collision between a ray and the scene.
		//Returns the id and the distance between the ray origin and the first RTPrimitive.
		//If the ray doesn't intersect with any RTPrimitive, i return -1.
		I32 getFirstCollision(const RTRay& ray, F32& dist);

		/*Add a primitive to the scene*/
		bool addPrimitive(RTIPrimitive* primitive);

		/*Add a light to the scene*/
		bool addLight(RTILight* light);

		/*Add a material to the scene*/
		bool addMaterial(RTMaterial* material, const string& matName);

		/*Add a procedural texture to the scene*/
		bool addProcTexture(RTIProcTexture* texture);

		//store a texture coming from a file
		bool addFileTexture(RTTexture* texture);

		//const RTColor computeLightColor(const RTVector3f& view, const RTPoint3f& center, const RTNormal3f& normal, 
		//	const RTIPrimitive& p);

		//Test if the ray collide with a RTPrimitive in the scene. 
		//dist is the length of the ray. A negative number means an infinite length.
		//return true if it collide or false if no collision is detected.
		bool testCollision(const RTRay& ray, F32 dist);

		/*Apply the view matrix to the scene*/
		void applyViewMatrix(const RTMatrix44& view);

		//Return the number of source lights in the scene
		U32 getNumberOfLights()const;

		//set the scene global ambient light
		void setGlobalAmbientLight(const RTColor& gal);

		//get the scene global ambient light
		RTColor getGlobalAmbientLight()const;

	};

	#define RTSCENE RTScene::getInstance()

}